﻿<controls:ChildWindow xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Name="childWindow"  x:Class="SharePointCSVImporter.FieldConfigurationPopup"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           MaxHeight="800"  xmlns:local="clr-namespace:SharePointCSVImporter"
           Title="CSV Field Mapping Configuration">

    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.Resources>
            <local:ListFields x:Key="listFieldOptions" />
            <local:TransformationValueConverter x:Key="TransformConverter" />
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <StackPanel>
            <Border BorderBrush="Black" BorderThickness="0,0,0,2" Margin="0,0,0,5">
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Destination Web Url:  "/>
                        <TextBox x:Name="TargetWebUrlTextBox" Text="{Binding Path=TargetWebUrl, Mode=TwoWay}" MinWidth="180"  />
                        <Button Margin="5,0,0,0"  x:Name="GetListsButton" Click="GetListsButton_Click"   Content="Get Lists"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0,5,5,5">
                        <TextBlock Text="Target List:               "/>
                        <ComboBox Name="ListsComboBox"  MinWidth="180" DisplayMemberPath="Title" SelectedValuePath="Title"  SelectedValue="{Binding Path=TargetListName, Mode=TwoWay}"  >

                        </ComboBox>
                        <Button Margin="5,0,0,0"  x:Name="MapButton" Click="MapButton_Click"  Content="Get Fields"/>
                        <StackPanel Margin="10,0,0,0" >
                            <CheckBox Name="OverWriteCheckBox" ToolTipService.ToolTip="If you want to update record check here and identify key columns"  Unchecked="OverWriteCheckBox_Unchecked" Checked="OverWriteCheckBox_Checked" IsChecked="{Binding Path=OverwriteExistingValues, Mode=TwoWay}" >Overwrite existing data with matching keys.</CheckBox>
                            <StackPanel Name="ActionStackPanel" Orientation="Horizontal">
                                <ComboBox x:Name="DuplicateAction"  Margin="0,0,5,0" SelectedIndex="{Binding Path=DuplicateKeyAction, Mode=TwoWay}">
                                    <ComboBoxItem Content="Do Nothing - Ignore new record." Tag="0"></ComboBoxItem>
                                    <ComboBoxItem Content="Update Latest - Duplicates Remain." Tag="1"></ComboBoxItem>
                                    <ComboBoxItem Content="Update Latest - Delete Duplicates." Tag="2"></ComboBoxItem>
                                </ComboBox>
                                <TextBlock Text="Select action if more than one existing matches key(s)" />
                            </StackPanel>
                        </StackPanel>

                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0,5,5,5" >
                        <TextBlock Text="Export/Import Mapping"/>
                        <Button Content="Import Mapping File" Margin="10,0,0,0" Name="ImportMappButton"  Click="ImportMappButton_Click"></Button>
                        <Button Content="Export Mapping File" Margin="10,0,0,0" Name="ExportMappButton" Click="ExportMappButton_Click"></Button>
                        <Button Content="Clear Mappings" Margin="10,0,0,0" Name="ClearMappingButton" Click="ClearMappingButton_Click"></Button>
                    </StackPanel>
                </StackPanel>

            </Border>
            <Border BorderBrush="Black" BorderThickness="0,0,0,2" >
                <ScrollViewer MaxHeight="600">
                    <sdk:DataGrid Name="MappingGrid" ItemsSource="{Binding Path=FieldMappings, Mode=TwoWay}"  ScrollViewer.VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" HorizontalAlignment="Left" MinWidth="500" >
                        <sdk:DataGrid.Columns>
                            <sdk:DataGridTextColumn Header="Index" Binding="{Binding Path=Index}" />
                            <sdk:DataGridTextColumn Header="CSV Column" Binding="{Binding Path=CsvColumnName}"  />
                            <sdk:DataGridTemplateColumn Header="Mapped SharePoint Field"    >
                                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate >
                                        <ComboBox Name="ListFieldCombo" SelectedValue="{Binding DestinationColumn, Mode=TwoWay}" SelectionChanged="ListFieldCombo_SelectionChanged"   SelectedValuePath="InternalName" ItemsSource="{Binding Fields, Source={StaticResource listFieldOptions}}">
                                            <ComboBox.ItemTemplate>
                                                <DataTemplate>
                                                    <StackPanel Orientation="Horizontal">

                                                        <TextBlock Text="{Binding Path=Title}"></TextBlock>
                                                        <TextBlock> (</TextBlock>
                                                        <TextBlock Text="{Binding Path=InternalName}"></TextBlock>
                                                        <TextBlock>)</TextBlock>
                                                    </StackPanel>
                                                </DataTemplate>
                                            </ComboBox.ItemTemplate>
                                        </ComboBox>

                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellEditingTemplate>
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Path=DestinationColumn}"></TextBlock>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                            </sdk:DataGridTemplateColumn>
                            <sdk:DataGridCheckBoxColumn Header="Is Key" Binding="{Binding Path=IsKeyColumn}"></sdk:DataGridCheckBoxColumn>
                            <sdk:DataGridTemplateColumn Header="Transformation">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Name="TransformRecordButton" Content="{Binding Path=Transformation, Converter={StaticResource TransformConverter}}" Click="TransformRecordButton_Click"  ></Button>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                            </sdk:DataGridTemplateColumn>
                        </sdk:DataGrid.Columns>

                    </sdk:DataGrid>
                </ScrollViewer>
            </Border>

        </StackPanel>
        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
    </Grid>
</controls:ChildWindow>